package com.blockmeta.bbs.businesslibrary.widget;

import android.content.Context;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.businesslibrary.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.pathview.PathsView;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.Random;

/**
 * Created by author on 2018/3/16.
 */

public class LogoRefreshView extends LinearLayout implements RefreshHeader {
    private TextView mHeaderText;//标题文本
    private PathsView mArrowView;//下拉箭头
    private ImageView mProgressView;//刷新动画视图
    private ImageView mLogoView;
    private GifDrawable mGifDrawable;
    private String[] script = {
            getResources().getString(R.string.refresh_script1),
            getResources().getString(R.string.refresh_script2),
            getResources().getString(R.string.refresh_script3),
            getResources().getString(R.string.refresh_script4),
            getResources().getString(R.string.refresh_script5),
            getResources().getString(R.string.refresh_script6),
            getResources().getString(R.string.refresh_script7),
            getResources().getString(R.string.refresh_script8),
            getResources().getString(R.string.refresh_script9),
            getResources().getString(R.string.refresh_script10),
            getResources().getString(R.string.refresh_script11),
            getResources().getString(R.string.refresh_script12),
            getResources().getString(R.string.refresh_script13),
            getResources().getString(R.string.refresh_script14),
            getResources().getString(R.string.refresh_script15),
            getResources().getString(R.string.refresh_script16),
            getResources().getString(R.string.refresh_script17),
            getResources().getString(R.string.refresh_script18),
            getResources().getString(R.string.refresh_script19),
            getResources().getString(R.string.refresh_script20),
            getResources().getString(R.string.refresh_script21),
            getResources().getString(R.string.refresh_script22),
            getResources().getString(R.string.refresh_script23),
            getResources().getString(R.string.refresh_script24),
            getResources().getString(R.string.refresh_script25),
            getResources().getString(R.string.refresh_script26),
            getResources().getString(R.string.refresh_script27),
            getResources().getString(R.string.refresh_script28),
            getResources().getString(R.string.refresh_script29),
            getResources().getString(R.string.refresh_script30),
            getResources().getString(R.string.refresh_script31),
            getResources().getString(R.string.refresh_script32),
            getResources().getString(R.string.refresh_script33),
            getResources().getString(R.string.refresh_script34),
            getResources().getString(R.string.refresh_script35),
            getResources().getString(R.string.refresh_script36),
            getResources().getString(R.string.refresh_script37),
            getResources().getString(R.string.refresh_script38),
            getResources().getString(R.string.refresh_script39),
            getResources().getString(R.string.refresh_script40),
            getResources().getString(R.string.refresh_script41),
            getResources().getString(R.string.refresh_script42),
            getResources().getString(R.string.refresh_script43),
            getResources().getString(R.string.refresh_script44),
            getResources().getString(R.string.refresh_script45),
            getResources().getString(R.string.refresh_script46),
            getResources().getString(R.string.refresh_script47),
            getResources().getString(R.string.refresh_script48),
            getResources().getString(R.string.refresh_script49),
            getResources().getString(R.string.refresh_script50),
    };


    public LogoRefreshView(Context context) {
        super(context);
        initView(context);
    }

    public LogoRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public LogoRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {

        setGravity(Gravity.CENTER);
        mHeaderText = new TextView(context);
        mHeaderText.setTextColor(getResources().getColor(R.color.text_999_gray));
        mHeaderText.setGravity(Gravity.CENTER_HORIZONTAL);

        mHeaderText.setTextSize(12);
        mLogoView = new ImageView(context);

        Glide.with(context).load(R.drawable.refresh_loading).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(mLogoView);

        mArrowView = new PathsView(context);
        mProgressView = new ImageView(context);
        mArrowView.parserPaths("M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z");
        addView(new View(context), DensityUtil.dp2px(75), DensityUtil.dp2px(15));
        addView(mLogoView, DensityUtil.dp2px(30), DensityUtil.dp2px(30));
        addView(new View(context), DensityUtil.dp2px(5), DensityUtil.dp2px(5));
        addView(mHeaderText, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addView(new View(context), DensityUtil.dp2px(75), DensityUtil.dp2px(15));
        setMinimumHeight(DensityUtil.dp2px(60));

        Random random = new Random();
        int i = random.nextInt(script.length - 1);
        mHeaderText.setText(script[i]);
    }


    @Override
    @NonNull
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        if (success) {
//            mHeaderText.setText("刷新完成");
        } else {
            mHeaderText.setText("刷新失败");
        }
//        mMObjectAnimator.cancel();
        mLogoView.setRotation(0);
        return 0;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        if(mGifDrawable == null){
            return;
        }
        //增添文字逻辑
        if (oldState.finishing){
            Random random = new Random();
            int i = random.nextInt(script.length - 1);
            mHeaderText.setText(script[i]);
        }
        switch (newState) {
            case None:
            case PullDownToRefresh:
                mArrowView.setVisibility(VISIBLE);//显示下拉箭头
                mProgressView.setVisibility(GONE);//隐藏动画
                mArrowView.animate().rotation(0);//还原箭头方向
                if (mGifDrawable.isRunning()){
                    mGifDrawable.stop();
                }
                break;
            case Refreshing:
                mProgressView.setVisibility(VISIBLE);//显示加载动画
                mArrowView.setVisibility(GONE);//隐藏箭头
                if (!mGifDrawable.isRunning()){
                    mGifDrawable.start();
                }
                break;
            case ReleaseToRefresh:
                mArrowView.animate().rotation(180);//显示箭头改为朝上
                break;
            default:
                break;
        }
    }


    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout layout, int height, int extendHeight) {
    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onReleasing(float percent, int offset, int height, int extendHeight) {
    }

    @Override
    public void onRefreshReleased(RefreshLayout layout, int headerHeight, int extendHeight) {

    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
    }
}
